package com.lecture.java8.http;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Xueqiu {

    public static void main(String[] args) {

http();

    }


    public static void http() {
        for (int i = 1; i < 57; i++) {
            String url = "https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page=" + i +"&size=90&order=desc&order_by=market_capital&market=CN&type=sh_sz";
            String cookie = "cookiesu=251711345915502; device_id=46f7f7f565770d2c0f41202b47c728e9; s=b71119i2ph; xq_a_token=f84a0b79c9e449cb1003cb36412faa34001a6697; xqat=f84a0b79c9e449cb1003cb36412faa34001a6697; xq_r_token=b24e38a4224932f5c7abd28126e8fc377b42755b; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTczMTgwNDgwNiwiY3RtIjoxNzI5ODQwNDU4NDI1LCJjaWQiOiJkOWQwbjRBWnVwIn0.m33eCfMkXSs32cfHNRHdxKMvInl7U4NItUuTMji6LG1Ii8jXOqd6y6mfsxswVlZ_WT9zxc67h0Mzh37m70FtKxLE0Ua8rjkXuSvXC5Y_HKChxg6PEpUrHtZQUEGt4PST82ltIT8rfNLbyy8hRkXwyh6l6p5DWP-ExjD0tpD11HoeV9Q9Vvo6z02Gkld7LULV7mmDQT0gc1R7lMkhtFvay_8Juv49UP8U3DlV4Hu8N4rbmwc2COp9aArHwtpVURTyt-0sfWi-Q_BpDQehArPVADrg4CzTfe7TeqOSg3Rck-UzzFLS7Qi2h9M6xKWBoFMiZjO2P_GTLHk6e_gMsdEuvg; u=251711345915502; Hm_lvt_1db88642e346389874251b5a1eded6e3=1729840509; HMACCOUNT=DCFB72EDF5DE711A; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1729840548; ssxmod_itna=CqfxBDuGGQituDBc4B4DKS34+OBQDn7rDS6x7qH55DskerDSxGKidDqxBWW=AhceddYQ+Dd+YBQ0RDF=tO2cEK=7pDeLADyex0aDbqGkQBAxiigDCeDIDWeDiDG4Gm4qGtDpxG=DjlUzzSP5Dbxi3VxGCsx0CezYD7hykEhYDhxDCulPDw6ACFDDzFQfPGC+OKCYDeZP+B6E1YASKklKD96YDsriUFl9pSRLP/EfjnFYdEx0k3q0OSoO1kbhCDUHhTGc4Gexid7ieK6x4k7iq+G0oCXDWKC44q0Gyd7hxdGDYRuryMkxDi6xd0pWGDxD; ssxmod_itna2=CqfxBDuGGQituDBc4B4DKS34+OBQDn7rDS6x7qH5ikLQvDlr8Dj4xrq62GPQidkjikq=KVDrTY44VimjY+sgl14SNWnlpqxALYxU0fXP45TmLnjxPMCQxp1/aTOHGcKYzxz4jL+W2Nd0HaD4WtVaH7epiTfOTW/WwFPcwNsh58/05i3xO7woKrGIgvhr8FIU0aOh7ww9aOVxgHe4Ok64fNtxbQeP9DYnuKY1BK0ji=qkPRqhOHdinFr2vEz90IqHqX=WoYlobrqY5=Bx+24oW5f+zrfKFSvn02BxWrQrDZvEfxokPeniYar41jQ/CIzBehC+=7Q8PUwpp1OD7CL9xwf7odUh9GEAjKsGowhrveQ7j2jnrT+rEObrpR9BdmxrBD2Sab/AeaapC8Re8rz=eLC6h0u=BGh05eQ2GWpPl5wcDmCWwD+Cjhm9ppQm8WbRiqvfrhDj+7QPcxR4qhiDeieKSrhjDbF9fnGu7j8GNdfmDKYk0T1ctnFNAhvhxor6pcEKb9pE0PnRiF1IotRmzto53F2uRA5xdf=mBnfIVnwRmnA7887Uap2bIdbffCOgw033o8k+GaDG2FGzSR8iKcG9HZ1dVfxezkxhfFuwMGTY0off21/7dGoTvruz1EWqmHmOAHOYyOBDD08DiQez4q7QKUxxD==="; // 设置Cookie值

            try {
                // 创建URL对象
                URL obj = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

                // 设置请求方法
                connection.setRequestMethod("GET");

                // 设置请求头
                connection.setRequestProperty("Cookie", cookie);
                connection.setRequestProperty("User-Agent", "Mozilla/5.0");
                connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

                // 获取响应码
                int responseCode = connection.getResponseCode();
                System.out.println("Response Code: " + responseCode);

                // 读取响应内容
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 输出响应内容
                //System.out.println("Response Body: " + response.toString());

                ObjectMapper objectMapper = new ObjectMapper();

                try {
                    ApiResponse response2 = objectMapper.readValue(response.toString(), ApiResponse.class);
                    System.out.println("Count: " + response2.getData().getCount());
                    for (ApiResponse.Data.Item item : response2.getData().getList()) {
                        if (item.getCurrent() < 10 ) {
                            System.out.println("股票代码：" + item.getSymbol() + "，股票名称：" + item.getName() + "，股票价格：" + item.getCurrent() + "，股票市值：" + formatNumber(item.getMarket_capital()));
                        }
                        // System.out.println("股票代码：" + item.getSymbol() + "，股票名称：" + item.getName() + "，股票价格：" + item.getCurrent() + "，股票市值：" + formatNumber(item.getMarket_capital()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static String formatNumber(double number) {

        if (number >= 100_000_000) {
            return String.format("%.2f亿", number / 100_000_000);
        } else if (number >= 10_000_000) {
            return String.format("%.2f千万", number / 10_000_000);
        } else if (number >= 10_000) {
            return String.format("%.2f万", number / 10_000);
        } else {
            return String.format("%.2f", number); // 小于1000直接输出
        }
    }
}
